//
// Created by shine on 2019/11/11.
//

#ifndef INC_360VRQE_SOFSAMPLER_H
#define INC_360VRQE_SOFSAMPLER_H

#include <iostream>
#include <opencv2/opencv.hpp>
#include "yuvReader.h"

using namespace std;
using namespace cv;


struct SpherePointPair{

    double startYaw;
    double startPitch;
    double endYaw;
    double endPitch;

    double sphereDistance;
};

struct SamplePointWithWeight{

    double yaw;
    double pitch;
    double weight;

};

struct SphericalCoordinatePoint{

    double yaw;
    double pitch;

};

struct RectangularCoordinatePoint{

    double x;
    double y;
    double z;

};

class SOFSampler {

    friend void yuvReader::Play();
    friend class OpticalFlowRenderer;

public:

    SOFSampler(const string& filePath, const string& width, const string& height);
    ~SOFSampler() = default;

    void OpticalFlowEst();

    inline bool isFlowCorrect(Point2f u)
    {
        return !cvIsNaN(u.x) && !cvIsNaN(u.y) && fabs(u.x) < 1e9 && fabs(u.y) < 1e9;
    }
    Vec3b computeColor(float fx, float fy);
    void drawOpticalFlow(const Mat_<Point2f>& flow, Mat& dst, float maxMotion = -1);
    void writeOpticalFlow(string prefix);
    void writeSamplePointsFile(string prefix);

    void slerp(double starting[4], double ending[4], double result[4], double t );
    void drawArrow(cv::Mat& img, cv::Point pStart, \
                    cv::Point pEnd, int len, int alpha,\
                    cv::Scalar& color, int thickness = 1, \
                    int lineType = 8);
    void drawOpticalFeature();
    void writeOpticalFeature(string prefix);


private:
    string _videoName;
    string _filePath;
    int _videoWidth;
    int _videoHeight;
    Size* _frameSize;
    int _framePixelNums;
    unsigned long _frameCount;
    ifstream _fin;

    FILE* _fileIn;
    Mat  _yuvFrame;
    int _frameIndex;
    Mat _preRGBFrame, _curRGBFrame;
    Mat _preGray, _curGray;
    Mat _flow, _flowOutput;

    Mat _preFeatureFrame, _curFeatureFrame;
    Mat _earthERP;

    Mat _depth;
    ofstream* PointFile;
};



#endif //INC_360VRQE_SOFSAMPLER_H
